(function (jQuery){
	ajaxpage = function(id, options){
		var defaults = {
	          	items_per_page:30,
	          	num_edge_entries:2,
	          	num_display_entries:5,
	          	next_text:'&#x4E0B;&#x4E00;&#x9875;',
	          	prev_text:'&#x4E0A;&#x4E00;&#x9875;',
	          	ellipse_text:'...',
	          	pagecontent:'#Searchresult',
	          	pagination:'#Pagination',
	          	ajaxurl:'',
	          	cookieid:'Select_Id_list',
	          	template:'',
	          	istips:0,//是否启用tips插件，默认不启用，如果启用需要调用插件mumayi.tips.js
	          	tipsclass:'.kylingoodtips',
	          	tipsattr:'tips',
	          	tipscolor:'tips_green',
	          	tipswidth:'350',
	          	tipsdirection:'top',
	          	iscountdown:0,//是否启用tips插件，默认不启用，如果启用需要调用插件mumayi.countdown.js
	          	timeclass:'.settime',
	          	timehtml:'endTime'
		};
		var id=id;
		var nowpage=$.cookie('Ajax_Page_Nums'); // get cookie
		//nowpage='';
		if(typeof(nowpage)=='string'){defaults.items_per_page=nowpage;}
		
		$.extend(defaults,options);

		//把json数据组合成html行数据
		var jsontohtml =function(data){
			 var html = defaults.template;
		     $.each(data,function(key,value){
		         html = html.replace(new RegExp('<{'+key+'}>',"gm"), data[key]);
		     })
			 return html;
	    };
	    var pageselectCallback=function(page_index){
	    	 	var htmls='';
	    	 	
	    	    $.post(defaults.ajaxurl,{page:page_index,counts:defaults.items_per_page},function(jsondata) {	
	    		  	if(jsondata.alldata){
		    	    	$.each( jsondata.alldata, function(key, val){
		    		  		htmls+=jsontohtml(val);
		    			});
		    		  	
		    		  	initPagination(jsondata.sum,page_index);
		    		  	if(jsondata.sum>0){
		    		  		$(defaults.pagecontent).empty().append(htmls);
	    		  		}else{
	    		  			$(defaults.pagecontent).empty().append('<tr><td colspan="12" align="center" height="40px">暂无相关数据</td></tr>');
	    		  		}

		    		  	$("tbody>tr").hover(function(){
			  	   		    $(this).addClass("trhover");
			  	   		  },
			  	   		 function(){
			  	   		    $(this).removeClass("trhover");
			  	   		 });
		    		  	
		    		  	$("tbody>tr:odd").addClass("altbg1");
		    		  	$("tbody>tr:even").addClass("altbg2");
		    		  	
		    		   //是否启用tips插件，默认不启用，如果启用需要调用插件mumayi.tips.js
		    		  	if(defaults.istips){
		    		  		$(defaults.tipsclass).Tips_attr(defaults.tipsattr, defaults.tipscolor, defaults.tipswidth, defaults.tipsdirection);
		    		  	}
		    		  	
		    		  	//是否启用tips插件，默认不启用，如果启用需要调用插件mumayi.countdown.js
		    		  	if(defaults.iscountdown){
			    		  	$("body").MumayiCountdown({    
			    				timeClass:defaults.timeclass,
			    				timeTtml:defaults.timehtml
			    			});
		    		  	}
		    		  	
		    			var oldidlist=newidlist=new Array();
		    			
		    			
		    		  	$("tbody>tr").click(
		    		  		  function () {
		    		  			var Arrlength=newidstring='';
		    		  			
		    		  			if($.cookie(defaults.cookieid)==null){
	    				  			var oldidlist=new Array();
	    				  		}else{
	    				  			var oldidlist=$.cookie(defaults.cookieid).split(",");
	    				  		}
		    		  			if($(this).attr("id")!=undefined){
		    		  				var ids=$(this).attr("id").replace("row_",''); 
			    		  			if($(this).is(".onchicked")){
			    		  				//$(this).find("input[type=checkbox]").attr("checked",false);
			    		  				//$(this).removeClass("onchicked");
			    		  				//从cookie中去掉此ID
			    		  				newidlist[0]=ids;
			    		  				newidstring=inANotInB(oldidlist,newidlist);
			    		   				
				    		  		}else{
				    		  			//$(this).find("input[type=checkbox]").attr("checked",true);
				    		  			//$(this).addClass("onchicked");
				    		  			Arrlength=oldidlist.length;
				    		  			//把新加的ID写入cookie
				    		  			oldidlist[Arrlength]=ids;
				    		  			newidstring=oldidlist
				    		  		}
									if($(this).find("input[type=checkbox]").attr("checked")){
										$(this).addClass("onchicked");
									}else{
										$(this).removeClass("onchicked");
									}

			    		  			$.cookie(defaults.cookieid,newidstring);
		    		  			}
		    		  		  }
		    		  	);
		    		  	
		    		    //如果没有值，就取空数组
		    			if($.cookie(defaults.cookieid)==null){
				  			idlists=new Array();
				  		}else{
				  			//idlists=$.cookie(defaults.cookieid).split(",");
                                                        idlists=new Array();
				  		}
		    			
		    			$.each(idlists,function(key,val){
		    				$("#checkbox_"+val).attr("checked", true);
		    				$("#checkbox_"+val).closest("tr").addClass("onchicked");
		    			});

	    		  	}else{
	    		  		
	    		  		$(defaults.pagecontent).empty().append('<tr><td colspan="12" align="center" height="40px">暂无相关数据</td></tr>');
	    		  	}

	    		},'json');
	    	   return false;
		};
		
		//注意jquery.pagination.js需要加入Ajaxpage.loadpage(current_page);
		var initPagination =function(sum,page_index){
			 $.extend(defaults,{current_page:page_index});
			 $(defaults.pagination).pagination(sum,defaults);
	    };
	    this.loadpage=function(page_index) {
                //$(id).empty().append('<div style="margin:0 auto;text-align:left;position: absolute;"><p><img src="/public/images/devload.gif" alt="数据正在载入中...." /><p></div>');
	    	pageselectCallback(page_index);
    	};
    	
		
    };
})(jQuery);

/* b与a不重复的部分的数组
 * 来源：http://topic.csdn.net/u/20110623/10/c49342ec-c282-4a2d-a0a6-9d2d98f5f485.html
 * */
function inANotInB(a,b){
	  var obj = new Object();
	  for(var i =0,len = a.length;i<len;i++){
	      obj[a[i]] = 1;
	  }
	  for(var i =0,len = b.length;i<len;i++){
	      if(obj.hasOwnProperty(b[i])){
	          obj[b[i]] = undefined;
	      }
	  }
	  var arr = new Array();
	  var i = 0; 
	  for(var per in obj){
	      if(obj[per]){
	          arr[i++] = per;
	      }
	  }
	  return arr;
}
  	